<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of sobjectproduce
 *
 * @author Dragon.hu add @2015.9.22
 */
defined('BASEPATH') or die('forbidden');

class Gem_Controller extends Controller {

	private $inc;
	private $logdb;	//数据库连接
	private $platformList;	//平台列表
	private $id2name;
	
	function _init() {
		check_level(50, segment(0), segment(1), segment(2, 'index'));
		set_time_limit(0);
		
		$this->inc = $this->config['inc'];
		$this->platformList = $this->inc['platform_list'];
		
        // log数据库
		//$this->logdb = $this->load->database('log_db');
		//得到选择的区号
		$areanum = $_SESSION['area_selected_id'];
		if ($areanum == '')
		{
			echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
			return;
		}
		// log数据库
        $this->logdb = $this->load->database('log_db');
        //$this->logdb = getTmpDbConnect(PLATFORM_ALL, 'all', $areanum, DBTYPE_LOG);
		if (false == $this->logdb) {
			//内部有提示错误，在此就不提示了
			 //echo "<script>alert(' connect areanum:$areanum error ');</script>";
            return;
		}

        $this->id2name = array(1=>'守望纹章',
			2=>'占卜宝珠',
			3=>'放逐纹章',
			4=>'智慧宝珠',
			5=>'英雄纹章',
			6=>'暗夜宝珠',
			7=>'勇士纹章',
			8=>'光辉宝珠',
			9=>'天行纹章',
			10=>'守护宝珠',
			11=>'先知纹章',
			12=>'奇迹宝珠',
			13=>'意志纹章',
			14=>'命运宝珠',
			15=>'荣耀纹章',
			16=>'神秘宝珠',
			17=>'圣堂纹章',
			18=>'上古宝珠',
			19=>'神铸纹章',
			20=>'神赐宝珠',
			21=>'稀有经验圣物',
			22=>'精致经验圣物',
			23=>'普通经验圣物',
		);
	}

	function index() {
		$this->view->display("log_object/gem/index");
	}

	function flexigrid() {
		list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
		$orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";

		$areanum = $_SESSION['area_selected_id'];
        if ($where == ""){
        	$where = "svrid=" . $areanum;
        }else{
        	$where = "svrid=" . $areanum . " and (" . $where . ")"; 
        }

        $mapAreaNum = ((int)$areanum)%5+1;
        //$table_name = "gamelog_9_".$mapAreaNum;
		$table_name = "gamelog_9";

        $sql = "SELECT COUNT(*) AS num FROM {$table_name} where {$where} ";
        $one = $this->logdb->one($sql);
        $total=$one['num'];

        $sql1 = "SELECT {$colsinfo} FROM {$table_name} where {$where}  {$orderby} LIMIT {$offset},{$rp}";
        $this->logdb->query($sql1);
        $result1=$this->logdb->row('default');

        foreach ($result1 as $k=>$n){
            $n['time']=date('Y/m/d H:i:s',$n['time']);
            $n['eventid']=$this->config['event'][$n['eventid']];
            $n['srcid']=$this->config['source'][$n['srcid']];
            $n['type']=$this->config['chtype'][$n['type']];
            //$n['param1']=$this->id2name[(int)$n['param1']];

            if (0 == (int)$n['param1']){ //多个装备用extra保存id列表
                $gemids=json_decode($n['extra'],true);
                $gemids_str = "";
                foreach ($gemids['Items'] as $key => $value) {
                    $gemname = $this->id2name[(int)$value['ID']];
                    $gemids_str = $gemids_str . $gemname . "*" . $value['Num'] . "  " ;
                }

                $n['param1']=$gemids_str;
            } else {
                $n['param1']=$this->id2name[(int)$n['param1']];
            }

            $n['extra']=($n['param2']>>32) & 0x00000000ffffffff;
            $n['param2'] = $n['param2'] & 0x00000000ffffffff;
            $rows[] = array('id' => $n['id'], 'cell' => $n);
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );

        echo json_encode($result);
	}

}
